package liuds.music.model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import liuds.music.App;
import liuds.music.utils.MusicUtils;
import liuds.music.utils.Utils;

/**
 * Created by Administrator on 2018/4/14.
 */

public class DbHelp extends SQLiteOpenHelper {
    private final static String DB_NAME = "music.db";
    private final static int VERSION = 1;
    private final static String SearchHistory = "SearchHistory";

    private static final DbHelp dbHelp = new DbHelp(App.getContext());

    private DbHelp(){
        super(null,null,null,0);
    };

    private DbHelp(Context context) {
        super(context, DB_NAME, null, VERSION);
    }

    public static DbHelp getInstance(){
        return dbHelp;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String searchHistory = String.format("create table %s (_id Integer primary key,title varchar)",SearchHistory);
        db.execSQL(searchHistory);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String searchHistory = String.format("drop table %s",SearchHistory);
        db.execSQL(searchHistory);
        onCreate(db);
    }


    public List<Map<String,Object>> getSearchHistory(){
        SQLiteDatabase sd=this.getWritableDatabase();
        Cursor cursor = sd.query(SearchHistory,null,null,null,null,null,null);
        List<Map<String,Object>> list = getListMap(cursor);
        sd.close();
        return list;
    }

    public void addSearchHistory(String title){
        SQLiteDatabase sd=this.getWritableDatabase();
        Cursor cursor = sd.query(SearchHistory,null,"title=?",new String[]{title},null,null,null);
        if(cursor.moveToNext()){
            return;
        }
        ContentValues cv = new ContentValues();
        cv.put("title",title);
        sd.insert(SearchHistory,null,cv);
        close();
    }

    public void delSearchHistory(int id){
        String sql = String.format("delete from %s where _id = %s", SearchHistory,id);
        SQLiteDatabase sd=this.getWritableDatabase();
        sd.execSQL(sql);
        sd.close();
    }

    public void clearSearchHistory(){
        String sql = String.format("delete from %s", SearchHistory);
        SQLiteDatabase sd=this.getWritableDatabase();
        sd.execSQL(sql);
        sd.close();
    }

    private List<Map<String,Object>> getListMap(Cursor cursor){
        List<Map<String,Object>> list = new ArrayList<>();
        while (cursor.moveToNext()){
            Map<String,Object> map = new HashMap<>();
            int columnCount = cursor.getColumnCount();
            for(int i=0;i<columnCount;i++){
                map.put(cursor.getColumnName(i),cursor.getString(i));
            }
            list.add(map);
        }
        return list;
    }
}
